package com.sc.nft.admin.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.admin.request.*;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.dao.DigitalCollectionDao;
import com.sc.nft.dao.DigitalCollectionProductInfoDao;
import com.sc.nft.dao.ShareProfitPanelDao;
import com.sc.nft.entity.*;
import com.sc.nft.entity.dto.AddAuctionProductDTO;
import com.sc.nft.entity.dto.AddSubscribeProductDTO;
import com.sc.nft.entity.dto.ModificationSubscribeProductDTO;
import com.sc.nft.entity.dto.ModifyAuctionProductDTO;
import com.sc.nft.entity.req.EcologyCastGasRequest;
import com.sc.nft.entity.req.SaveProductRuleConsensusReq;
import com.sc.nft.entity.vo.*;
import com.sc.nft.entity.vo.ecology.AdminUserEcologyCastUpperLimitDetailsVO;
import com.sc.nft.enums.*;
import com.sc.nft.exception.GlobalException;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.*;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/product")
@Api(tags = "后台商品模块")
public class DigitalCollectionProductInfoAdminController {

    private final DigitalCollectionProductInfoService digitalCollectionProductInfoService;

    private final BoxProductService boxProductService;

    private final DigitalCollectionDao digitalCollectionDao;
    
    private final PrivilegeRuleService privilegeRuleService;

    private final PrivilegeService privilegeService;

    private final BuyFirstCollectionService buyFirstCollectionService;

    private final BuyFirstUserService buyFirstUserService;

    private final DigitalCollectionService digitalCollectionService;

    private final UserInfoService userInfoService;

    private final RushBuyCollectionService rushBuyCollectionService;

    private final BuyCollectionDiscountService buyCollectionDiscountService;

    private final PushDetailsService pushDetailsService;

    private final RushBuyUserService rushBuyUserService;

    private final DigitalCollectionProductInfoDao digitalCollectionProductInfoDao;

    private final Redisson redisson;

    private final ShareProfitPanelDao shareProfitPanelDao;


    @PostMapping(value = "/addProduct", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "创建商品", notes = "创建商品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result addProduct(@Valid @RequestBody AddProductRequest request) {
        request.checkProductType();
        if (request.getIsSmeltingOnly() != null && request.getIsSmeltingOnly()) {
            if (request.getIsLockedPosition()) {
                throw new GlobalRunTimeException("锁仓和仅熔炼无法同时配置");
            }
            if (request.getCommodityType() != CommodityType.PRODUCT
                    && request.getCommodityType() != CommodityType.AIR_DROP
                    && request.getCommodityType() != CommodityType.SYNTHESIS
                    && request.getCommodityType() != CommodityType.EXCHANGE_COLLECTION) {
                throw new GlobalRunTimeException("仅普通商品和空投商品新增是否仅熔炼配置");
            }
        }
        // 锁仓判定
        if (!request.lockedPositionDecide()) {
            throw new GlobalRunTimeException(ErrorCode.LOCKED_POSITION);
        }
        DigitalCollectionProductInfo productInfo = BeanUtil.toBean(request, DigitalCollectionProductInfo.class);
        EcologyCastGasRequest ecologyCastGas = request.getEcologyCastGas();
        if (Objects.nonNull(ecologyCastGas)){
            productInfo.setGasConf(ecologyCastGas.getGasConf());
            if (ecologyCastGas.getGasConf()){
                productInfo.setConsumeCost(ecologyCastGas.getConsumeCost());
                productInfo.setConsumeType(ecologyCastGas.getConsumeType());
            }
        }
       
        ProductStock productStock = new ProductStock();
        productInfo.setStatus(false);
        productInfo.setIsEnable(false);
        productInfo.setIsSoldOut(false);

        if (request.getCommodityType() == CommodityType.PRODUCT
                || request.getCommodityType() == CommodityType.EXCHANGE_COLLECTION
                || request.getCommodityType() == CommodityType.CDK_PRODUCT) {
            Assert.notNull(request.getIsRegisterTime(), () -> new GlobalRunTimeException("是否开启注册时间限制"));
        } else {
            request.setIsRegisterTime(Boolean.FALSE);
        }

        if (request.getCommodityType() == CommodityType.PRODUCT) {
            if (ObjectUtil.isNull(request.getShareProfitPanelId())) {
                return Result.fail("请选择分润模板");
            }
            ShareProfitPanel panel = shareProfitPanelDao.getById(request.getShareProfitPanelId());
            if (ObjectUtil.isNull(panel)) {
                return Result.fail("未找到分润模板");
            }
            productInfo.setShareProfitPanelId(panel.getId());

            if (request.getIsRegisterTime()) {
                Assert.isFalse(request.getIsPriorityPurchase(),
                        () -> new GlobalRunTimeException("优先购商品不能设置注册时间限制"));
                Assert.isFalse(request.getIsRushBuy(),
                        () -> new GlobalRunTimeException("优先抢商品不能设置注册时间限制"));

                if (ObjectUtil.isNull(request.getRegisterEndTime()) || ObjectUtil.isNull(
                        request.getRegisterBeginTime())) {
                    return Result.fail("注册限制时间不能为空");
                }

                if (request.getRegisterBeginTime().after(request.getRegisterEndTime())) {
                    return Result.fail("注册开始时间不能在结束时间之后");
                }

                productInfo.setIsRegisterTime(request.getIsRegisterTime());
                productInfo.setRegisterBeginTime(request.getRegisterBeginTime());
                productInfo.setRegisterEndTime(request.getRegisterEndTime());
            }
        }
        //兑换商品
        if (request.getCommodityType() == CommodityType.EXCHANGE_COLLECTION) {
            //设置注册开始时间和结束时间
            if (request.getIsRegisterTime()) {
                if (ObjectUtil.isNull(request.getRegisterEndTime()) || ObjectUtil.isNull(
                        request.getRegisterBeginTime())) {
                    return Result.fail("注册限制时间不能为空");
                }

                if (request.getRegisterBeginTime().after(request.getRegisterEndTime())) {
                    return Result.fail("注册开始时间不能在结束时间之后");
                }

                productInfo.setIsRegisterTime(request.getIsRegisterTime());
                productInfo.setRegisterBeginTime(request.getRegisterBeginTime());
                productInfo.setRegisterEndTime(request.getRegisterEndTime());
            }
        }
        //CDK商品
        if (request.getCommodityType() == CommodityType.CDK_PRODUCT) {
            if (request.getIsRegisterTime()) {

                if (ObjectUtil.isNull(request.getRegisterEndTime()) || ObjectUtil.isNull(
                        request.getRegisterBeginTime())) {
                    return Result.fail("注册限制时间不能为空");
                }

                if (request.getRegisterBeginTime().after(request.getRegisterEndTime())) {
                    return Result.fail("注册开始时间不能在结束时间之后");
                }

                productInfo.setIsRegisterTime(request.getIsRegisterTime());
                productInfo.setRegisterBeginTime(request.getRegisterBeginTime());
                productInfo.setRegisterEndTime(request.getRegisterEndTime());
            }
        }
        CatenaInfo catenaInfo = (CatenaInfo) new CatenaInfo().selectById(productInfo.getCatenaId());
        if (ObjectUtil.isNotEmpty(catenaInfo)) {
            productInfo.setCatenaName(catenaInfo.getName());
        }
        long snowflakeNextId = IdUtil.getSnowflakeNextId();
        productInfo.setDigitalCollectionProductInfoUuid("nft" + String.valueOf(snowflakeNextId));
        if (request.getIsPriorityPurchase()) {

            Assert.isFalse(request.getIsRegisterTime(), () -> new GlobalRunTimeException("优先购不能设置注册时间限制"));

            if (ObjectUtil.isEmpty(request.getPriorityPurchaseBeginTime()) || ObjectUtil.isEmpty(
                    request.getPriorityPurchaseEndTime())) {
                return Result.fail("优先购商品必须设置优先购开始结束时间");
            }
            if (request.getPriorityPurchaseBeginTime().after(request.getBeginTime())
                    || request.getPriorityPurchaseEndTime().after(request.getBeginTime())) {
                return Result.fail("优先购开始或结束时间不能在商品开启购买时间之后");
            }
            if (request.getPriorityPurchaseBeginTime().after(request.getPriorityPurchaseEndTime())) {
                return Result.fail("优先购开始时间不能在优先购结束时间之后");
            }
        }
        //优先抢商品
        if (request.getIsRushBuy()) {

            Assert.isFalse(request.getIsRegisterTime(), () -> new GlobalRunTimeException("优先抢不能设置注册时间限制"));

            if (ObjectUtil.isEmpty(request.getRushBuyBeginTime()) || ObjectUtil.isEmpty(request.getRushBuyEndTime())) {
                return Result.fail("优先抢商品必须设置优先抢开始结束时间");
            }
            if (request.getIsPriorityPurchase()) {
                if (request.getRushBuyBeginTime().equals(request.getPriorityPurchaseEndTime())
                        || request.getRushBuyEndTime().before(request.getPriorityPurchaseEndTime())) {
                    return Result.fail("优先抢开始或结束时间不能在优先购结束时间之前");
                }
            }
            if (request.getRushBuyBeginTime().after(request.getBeginTime()) || request.getRushBuyEndTime()
                    .after(request.getBeginTime())) {
                return Result.fail("优先抢开始或结束时间不能在商品开启购买时间之后");
            }
            if (request.getRushBuyBeginTime().after(request.getRushBuyEndTime())) {
                return Result.fail("优先抢开始时间不能在优先抢结束时间之后");
            }
        }
        if (CommodityType.CDK_PRODUCT == request.getCommodityType()) {
            if (ObjectUtil.isEmpty(request.getBeginTime())) {
                return Result.fail("兑换码商品开始时间是必填");
            }
            if (ObjectUtil.isEmpty(request.getEndTime())) {
                return Result.fail("兑换码商品结束时间是必填");
            }
            if (request.getBeginTime().after(request.getEndTime())) {
                return Result.fail("兑换码开始时间要在结束时间之前");
            }
        }

        if (ObjectUtil.isNotNull(productInfo.getEquityPropsId()) && productInfo.getEquityPropsId() != 0) {
            if (productInfo.getEquityPropsNum() == 0) {
                return Result.fail("权益商品数量不能为0");
            }
        }

        if (CommodityType.SALVAGE_COLLECTION == request.getCommodityType()) {
            productInfo.setIsPriorityPurchase(Boolean.FALSE);//优先购
            productInfo.setIsRushBuy(Boolean.FALSE);//优先抢
            productInfo.setIsUpgrade(Boolean.FALSE);//升级藏品
            Assert.notNull(request.getShowSalvagePrefecture(), () -> new GlobalRunTimeException("展示专区不能为空"));
            productInfo.setShowSalvagePrefecture(request.getShowSalvagePrefecture());
        }

        if (CommodityType.ECOLOGY_COMMODITY == request.getCommodityType()) {
            productInfo.setIsPriorityPurchase(Boolean.FALSE);//优先购
            productInfo.setIsRushBuy(Boolean.FALSE);//优先抢
            productInfo.setIsUpgrade(Boolean.FALSE);//升级藏品
            Assert.isTrue(ObjectUtil.isNotEmpty(request.getEcologyCastPresentation()),
                    () -> new GlobalRunTimeException("生态铸造须知不能为空"));
        }
        if (CommodityType.ECOLOGY_REFORGE == request.getCommodityType()) {
            productInfo.setIsPriorityPurchase(Boolean.FALSE);//优先购
            productInfo.setIsRushBuy(Boolean.FALSE);//优先抢
            productInfo.setIsUpgrade(Boolean.FALSE);//升级藏品
            //与铸造须知公用一个字段
            Assert.isTrue(ObjectUtil.isNotEmpty(request.getEcologyCastPresentation()),
                    () -> new GlobalRunTimeException("生态铸造须知不能为空"));
        }

        switch (productInfo.getTypeClassify()) {

            case COLLECTION:
                DigitalCollection digitalCollection = digitalCollectionDao.getById(productInfo.getRelationId());

                if (digitalCollection.getChainStatus() != CollectionChainStatusEnum.CHAIN_COMPLETED) {
                    return Result.fail("该藏品未上链，不能创建商品");
                }

                if (request.getProductCount() <= 0) {
                    return Result.fail("商品数量不正确");
                }
                if (request.getProductCount() > digitalCollection.getUsableCount()) {
                    return Result.fail("藏品库存不足");
                }
                if (productInfo.getCommodityType() == CommodityType.PRODUCT) {
                    productInfo.setAppleProductId(AppleProductEnum.SEVENTY_THREE.getId());
                }
                if (CommodityType.ECOLOGY_COMMODITY == request.getCommodityType()) {
                    Assert.isTrue(digitalCollection.getCollectionType() == DigitalMarketTypeEnum.ECOLOGY,
                            () -> new GlobalRunTimeException("必须是生态藏品才能添加生态铸造"));

                }

                //可合成的藏品校验是否设置合成规则
                if (digitalCollection.getIsSynthesis() && (request.getCommodityType().equals(CommodityType.SYNTHESIS)
                        || request.getCommodityType().equals(CommodityType.PRIVILEGE))) {
                    if (ObjectUtil.isNull(request.getSynthesisCount())) {
                        return Result.fail("合成数量不能为空");
                    }

                    if (request.getSynthesisCount() <= 0) {
                        return Result.fail("合成数量不能小于0");
                    }

                    if (ObjectUtil.isEmpty(request.getEndTime()) || ObjectUtil.isEmpty(request.getBeginTime())) {
                        return Result.fail("合成商品特权商品必须设置开始时间和结束时间");
                    }

                    //设置合成数量
                    productInfo.setSynthesisCount(request.getSynthesisCount());
                }
                //特权藏品校验是否设置权益
                if (digitalCollection.getIsPrivilege()) {
                    List<Privilege> list = privilegeService.getByCollectionId(digitalCollection.getId());
                    if (list.size() <= 0) {
                        return Result.fail("特权藏品未设置权益");
                    }
                }

                Assert.isTrue(digitalCollection.getOriginalType() == CollectionOriginalTypeEnum.MATE_UNIVERSE,
                        () -> new GlobalRunTimeException("玛特钱包的藏品不能添加到商品"));
                productInfo.setIsUpgrade(digitalCollection.getIsUpgrade());
                productInfo.setDaoLevel(digitalCollection.getDaoLevel());
                productInfo.setUserCreatorId(digitalCollection.getUserCreatorId());
                productInfo.setLevel(digitalCollection.getLevel().getCode());
                productInfo.setIsUpgrade(digitalCollection.getIsUpgrade());
                productInfo.setDaoLevel(digitalCollection.getDaoLevel());
                productInfo.setModelImg(digitalCollection.getModelImg());
                productInfo.setModelDistance(digitalCollection.getModelDistance());
                productStock.setSellCount(0);
                productStock.setFrozenCount(0);
                productStock.setUsableCount(request.getProductCount());
                productStock.setSumCount(request.getProductCount());
                productStock.setStockType(StockType.COLLECTION);
                productStock.setRelationId(request.getRelationId());
                productInfo.setThreeModelId(digitalCollection.getThreeModelId());
                productInfo.setSkipUrl(digitalCollection.getSkipUrl());
                productStock.setVersion(0);
                break;
            case BLIND_BOX:
                BoxProduct boxProduct = boxProductService.getById(request.getRelationId());
                productInfo.setUserCreatorId(boxProduct.getUserCreatorId());
                productInfo.setAboutBox(boxProduct.getAboutBox());
                productInfo.setBoxDetailsPicture(boxProduct.getBoxDetailsPicture());
                productInfo.setProductPresentation(boxProduct.getProductPresentation());
                if (productInfo.getCommodityType() == CommodityType.PRODUCT) {
                    productInfo.setAppleProductId(AppleProductEnum.BLINDBOX.getId());
                }
                if (BooleanUtil.isFalse(boxProduct.getStatus())) {
                    return Result.fail("盲盒未上架");
                }
                productStock.setStockType(StockType.BLIND_BOX);
                productStock.setSellCount(0);
                productStock.setFrozenCount(0);
                productStock.setUsableCount(boxProduct.getBoxCount());
                productStock.setRelationId(request.getRelationId());
                productStock.setSumCount(boxProduct.getBoxCount());
                productStock.setVersion(0);
                break;
        }

        digitalCollectionProductInfoService.addProduct(productInfo, productStock);

        return Result.success();
    }

    @PostMapping(value = "/commodityType", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "商品类型", notes = "商品类型", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result commodityType() {
        CommodityType[] values = CommodityType.values();
        List<Integer> filtration = new ArrayList<>();
        filtration.add(13);
        List list = new ArrayList();
        for (CommodityType value : values) {
            if (filtration.contains(value.getCode())) {
                continue;
            }
            Map map = new HashMap();
            map.put("name", value);
            map.put("vale", value.getDescription());
            map.put("code", value.getCode());
            list.add(map);
        }
        return Result.success(list);
    }

    @PostMapping(value = "/classifyType", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "商品分类", notes = "商品分类", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result classifyType() {
        return Result.success(ClassifyType.values());
    }


    @PostMapping(value = "/pageProduct", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "商品列表", notes = "商品列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<PageProductVO> pageProduct(@Valid @RequestBody PageProductRequest pageProductRequest) {
        Page<PageProductVO> page = digitalCollectionProductInfoService.pageProduct(pageProductRequest.getFirstTitle(),
                pageProductRequest.getProductUuid(), pageProductRequest.getStatus(), pageProductRequest.getIsSoldOut(),
                pageProductRequest.getCommodityType(), pageProductRequest.getClassifyType(),
                pageProductRequest.getPageNo(), pageProductRequest.getPageSize());
        return PageResult.success(page);
    }

    @PostMapping(value = "/updateConcernedNum", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改排序", notes = "修改排序", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result updateConcernedNum(@Valid @RequestBody updateConcernedNumRequest request) {
        digitalCollectionProductInfoService.updateConcernedNumRequest(request.getId(), request.getConcernedNum());
        return Result.success();
    }

    @PostMapping(value = "/updateStock", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改库存", notes = "修改库存", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result updateStock(@Valid @RequestBody updateStockRequest request) {
        RLock lock = redisson.getLock(RedisKeyEnum.EDIT_PRODUCT_STOCK_LOCK.getKey() + request.getId());
        try {
            boolean res = lock.tryLock(3, TimeUnit.SECONDS);
            if (res) {
                digitalCollectionProductInfoService.updateStock(request.getId(), request.getIsAdd(),
                        request.getNumber());
            } else {
                return Result.fail(ErrorCode.LATER_TRY);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            throw new GlobalRunTimeException("系统超时");
        } finally {
            lock.unlock();
        }
        return Result.success();
    }

    @PostMapping(value = "/soldOut", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "一键售罄", notes = "一键售罄", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result soldOut(@Valid @RequestBody IdConfirmRequest request) {
        RLock lock = redisson.getLock(RedisKeyEnum.EDIT_SOLD_OUT_LOCK.getKey() + request.getId());
        try {
            boolean res = lock.tryLock(3, TimeUnit.SECONDS);
            if (res) {
                digitalCollectionProductInfoService.soldOut(request.getId());
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            throw new GlobalRunTimeException("系统超时");
        } finally {
            lock.unlock();
        }
        return Result.success();
    }

    @PostMapping(value = "/updateProduct", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改商品", notes = "修改商品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result updateProduct(@Valid @RequestBody UpdateProductRequest request) {
        DigitalCollectionProductInfo digitalCollectionProductInfo = (DigitalCollectionProductInfo) new DigitalCollectionProductInfo().selectById(
                request.getProductId());
        if (ObjectUtil.isEmpty(digitalCollectionProductInfo)) {
            throw new GlobalRunTimeException(ErrorCode.PRODUCT_NOT_FOUND);
        }

        if (digitalCollectionProductInfo.getCommodityType() == CommodityType.PRODUCT
                || digitalCollectionProductInfo.getCommodityType() == CommodityType.EXCHANGE_COLLECTION
                || digitalCollectionProductInfo.getCommodityType() == CommodityType.CDK_PRODUCT) {
            Assert.notNull(request.getIsRegisterTime(), () -> new GlobalRunTimeException("是否开启注册时间限制"));
        } else {
            request.setIsRegisterTime(Boolean.FALSE);
        }

        if (!digitalCollectionProductInfo.getIsSmeltingOnly().equals(request.getIsSmeltingOnly())) {
            throw new GlobalRunTimeException("仅熔炼选项不支持修改");
        }

        if (digitalCollectionProductInfo.getCommodityType() == CommodityType.SYNTHESIS) {
            if (!digitalCollectionProductInfo.getSynthesisCount().equals(request.getSynthesisCount())) {
                throw new GlobalRunTimeException("合成上限不支持修改");
            }
        }

        if (digitalCollectionProductInfo.getStatus()) {
            return Result.fail("商品上架中不可做修改");
        }
        if (digitalCollectionProductInfo.getCommodityType() == CommodityType.PRODUCT) {
            if (ObjectUtil.isNull(request.getShareProfitPanelId())) {
                return Result.fail("请选择分润模板");
            }
            ShareProfitPanel panel = shareProfitPanelDao.getById(request.getShareProfitPanelId());
            if (ObjectUtil.isNull(panel)) {
                return Result.fail("未找到分润模板");
            }
            if (!panel.getStatus()) {
                return Result.fail("该模板未上架");
            }
            //设置注册开始时间和结束时间
            if (request.getIsRegisterTime()) {
                Assert.isFalse(request.getIsPriorityPurchase(),
                        () -> new GlobalRunTimeException("优先购商品不能设置注册时间限制"));
                Assert.isFalse(request.getIsRushBuy(),
                        () -> new GlobalRunTimeException("优先抢商品不能设置注册时间限制"));

                if (ObjectUtil.isNull(request.getRegisterEndTime()) || ObjectUtil.isNull(
                        request.getRegisterBeginTime())) {
                    return Result.fail("注册限制时间不能为空");
                }

                if (request.getRegisterBeginTime().after(request.getRegisterEndTime())) {
                    return Result.fail("注册开始时间不能在结束时间之后");
                }

            }

            digitalCollectionProductInfo.setShareProfitPanelId(panel.getId());
        }

        //兑换商品
        if (digitalCollectionProductInfo.getCommodityType() == CommodityType.EXCHANGE_COLLECTION) {
            //设置注册开始时间和结束时间
            if (request.getIsRegisterTime()) {
                if (ObjectUtil.isNull(request.getRegisterEndTime()) || ObjectUtil.isNull(
                        request.getRegisterBeginTime())) {
                    return Result.fail("注册限制时间不能为空");
                }

                if (request.getRegisterBeginTime().after(request.getRegisterEndTime())) {
                    return Result.fail("注册开始时间不能在结束时间之后");
                }
            }
        }

        //CDK兑换商品
        if (digitalCollectionProductInfo.getCommodityType() == CommodityType.CDK_PRODUCT) {
            //设置注册开始时间和结束时间
            if (request.getIsRegisterTime()) {

                if (ObjectUtil.isNull(request.getRegisterEndTime()) || ObjectUtil.isNull(
                        request.getRegisterBeginTime())) {
                    return Result.fail("注册限制时间不能为空");
                }

                if (request.getRegisterBeginTime().after(request.getRegisterEndTime())) {
                    return Result.fail("注册开始时间不能在结束时间之后");
                }

            }
        }

        if (digitalCollectionProductInfo.getCommodityType() == CommodityType.SALVAGE_COLLECTION) {
            digitalCollectionProductInfo.setShowSalvagePrefecture(request.getShowSalvagePrefecture());
        }
        if (digitalCollectionProductInfo.getCommodityType() == CommodityType.ECOLOGY_COMMODITY
                || digitalCollectionProductInfo.getCommodityType() == CommodityType.ECOLOGY_REFORGE) {
            digitalCollectionProductInfo.setEcologyCastPresentation(request.getEcologyCastPresentation());
        }

        if (ObjectUtil.isNotEmpty(request.getUpperLimit()) && request.getUpperLimit() <= 0) {
            return Result.fail("购买上限不能小于等于0");
        }

        if (ObjectUtil.isEmpty(request.getBeginTime()) || ObjectUtil.isEmpty(request.getEndTime())) {
            return Result.fail("商品必须设置开始和结束时间");
        }
        if (request.getBeginTime().after(request.getEndTime())) {
            return Result.fail("商品的开始时间要在结束时间之前");
        }

        if (digitalCollectionProductInfo.getEndTime() != request.getEndTime() && DateTime.now()
                .isAfter(digitalCollectionProductInfo.getEndTime())) {
            return Result.fail("商品已过期，不能修改");
        }

        //优先购商品校验时间
        if (request.getIsPriorityPurchase()) {

            Assert.isFalse(request.getIsRegisterTime(),
                    () -> new GlobalRunTimeException("优先购商品不能设置注册时间限制"));

            if (ObjectUtil.isEmpty(request.getPriorityPurchaseBeginTime()) || ObjectUtil.isEmpty(
                    request.getPriorityPurchaseEndTime())) {
                return Result.fail("优先购商品必须设置优先购开始结束时间");
            }
            if (request.getPriorityPurchaseBeginTime().after(request.getBeginTime())
                    || request.getPriorityPurchaseEndTime().after(request.getBeginTime())) {
                return Result.fail("优先购开始或结束时间不能在商品开启购买时间之后");
            }
            if (request.getPriorityPurchaseBeginTime().after(request.getPriorityPurchaseEndTime())) {
                return Result.fail("优先购开始时间不能在优先购结束时间之后");
            }
            if (ObjectUtil.isEmpty(request.getPriorityPurchaseLimit()) || request.getPriorityPurchaseLimit() <= 0) {
                return Result.fail("优先购商品必须设置优先购上限");
            }
        }
        //优先抢时间
        if (request.getIsRushBuy()) {
            Assert.isFalse(request.getIsRegisterTime(),
                    () -> new GlobalRunTimeException("优先抢商品不能设置注册时间限制"));

            if (ObjectUtil.isEmpty(request.getRushBuyBeginTime()) || ObjectUtil.isEmpty(request.getRushBuyEndTime())) {
                return Result.fail("优先抢商品必须设置优先抢开始结束时间");
            }
            if (request.getIsPriorityPurchase()) {
                if (request.getRushBuyBeginTime().equals(request.getPriorityPurchaseEndTime())
                        || request.getRushBuyEndTime().before(request.getPriorityPurchaseEndTime())) {
                    return Result.fail("优先抢开始或结束时间不能在优先购结束时间之前");
                }
            }
            if (request.getRushBuyBeginTime().after(request.getBeginTime()) || request.getRushBuyEndTime()
                    .after(request.getBeginTime())) {
                return Result.fail("优先抢开始或结束时间不能在商品开启购买时间之后");
            }
            if (request.getRushBuyBeginTime().after(request.getRushBuyEndTime())) {
                return Result.fail("优先抢开始时间不能在优先抢结束时间之后");
            }
            if (ObjectUtil.isEmpty(request.getRushBuyLimit()) || request.getRushBuyLimit() <= 0) {
                return Result.fail("优先抢商品必须设置优先抢次数");
            }
        }
        digitalCollectionProductInfo.setCollectionsConcernedNum(request.getCollectionsConcernedNum());
        if (request.getEquityPropsId() != 0 && request.getEquityPropsNum() == 0) {
            return Result.fail("权益商品数量不能为0");
        }
        digitalCollectionProductInfo.setEquityPropsId(request.getEquityPropsId());
        digitalCollectionProductInfo.setEquityPropsNum(request.getEquityPropsNum());

        digitalCollectionProductInfo.setFirstTitle(request.getFirstTitle());
        //        digitalCollectionProductInfo.setProductType(request.getProductType());
        digitalCollectionProductInfo.setProductNum(request.getProductNum());
        digitalCollectionProductInfo.setIntegralNum(request.getIntegralNum());
        digitalCollectionProductInfo.setBeginTime(request.getBeginTime());
        digitalCollectionProductInfo.setEndTime(request.getEndTime());
        digitalCollectionProductInfo.setUpperLimit(request.getUpperLimit());
        digitalCollectionProductInfo.setIsPriorityPurchase(request.getIsPriorityPurchase());
        digitalCollectionProductInfo.setPriorityPurchaseLimit(request.getPriorityPurchaseLimit());
        digitalCollectionProductInfo.setPriorityPurchaseAllLimit(request.getPriorityPurchaseAllLimit());
        digitalCollectionProductInfo.setPriorityPurchaseBeginTime(request.getPriorityPurchaseBeginTime());
        digitalCollectionProductInfo.setPriorityPurchaseEndTime(request.getPriorityPurchaseEndTime());
        digitalCollectionProductInfo.setIsRushBuy(request.getIsRushBuy());
        digitalCollectionProductInfo.setRushBuyLimit(request.getRushBuyLimit());
        digitalCollectionProductInfo.setRushBuyBeginTime(request.getRushBuyBeginTime());
        digitalCollectionProductInfo.setRushBuyEndTime(request.getRushBuyEndTime());
        digitalCollectionProductInfo.setPlatformShow(request.getPlatformShow());
        digitalCollectionProductInfo.setBrandShow(request.getBrandShow());
        digitalCollectionProductInfo.setFirmShow(request.getFirmShow());
        digitalCollectionProductInfo.setIsRegisterTime(request.getIsRegisterTime());
        digitalCollectionProductInfo.setRegisterBeginTime(request.getRegisterBeginTime());
        digitalCollectionProductInfo.setRegisterEndTime(request.getRegisterEndTime());
        digitalCollectionProductInfo.updateById();

        return Result.success();
    }

    @PostMapping(value = "/updateProductStatus", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改商品状态", notes = "修改商品状态", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result updateProductStatus(@Valid @RequestBody UpdateProductStatusRequest request) {
        digitalCollectionProductInfoService.updateProductStatus(request.getId(), request.getStatus(),
                request.getIsEnable());
        return Result.success();
    }

    @PostMapping(value = "/relatedCollectionsList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "优先购关联的藏品列表", notes = "优先购关联的藏品列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<List<RelatedCollectionsListVO>> relatedCollectionsList(@Valid @RequestBody IdRequest request) {
        List<RelatedCollectionsListVO> list = buyFirstCollectionService.relatedCollectionsList(request.getId());
        return Result.success(list);
    }

    @PostMapping(value = "/deleteRelatedCollections", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除优先购商品关联的藏品", notes = "删除优先购商品关联的藏品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result deleteRelatedCollections(@Valid @RequestBody IdRequest request) {
        new BuyFirstCollection().deleteById(request.getId());
        return Result.success();
    }

    @PostMapping(value = "/simpleList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "简单藏品列表", notes = "简单藏品列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<List<CollectionSimpleVO>> simpleList(@Valid @RequestBody RelatedCollectionRequest request) {
        Page<CollectionSimpleVO> page = digitalCollectionService.pageRelatedCollections(request.getFirstTitle(),
                request.getPageNo(), request.getPageSize());
        return PageResult.success(page);
    }


    @PostMapping(value = "/simpleProductList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "简单商品列表", notes = "简单商品列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<List<ProductSimpleVO>> simpleProductList(@Valid @RequestBody NameRequest request) {
        List<DigitalCollectionProductInfo> digitalCollectionProductInfos = digitalCollectionProductInfoDao.listByNameWithDaoId(
                request.getFirstTitle(), request.getId());
        return Result.successColl(digitalCollectionProductInfos, ProductSimpleVO.class);
    }

    @PostMapping(value = "/addRelatedCollections", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "设置优先购商品关联的藏品", notes = "设置优先购商品关联的藏品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result addRelatedCollections(@Valid @RequestBody AddRelatedCollectionRequest request) {
        DigitalCollectionProductInfo digitalCollectionProductInfo = (DigitalCollectionProductInfo) new DigitalCollectionProductInfo().selectById(
                request.getProductId());
        if (ObjectUtil.isEmpty(digitalCollectionProductInfo)) {
            return Result.fail("未找到商品请稍后重试");
        }
        if (!digitalCollectionProductInfo.getIsPriorityPurchase()) {
            return Result.fail("该商品不是优先购商品不能设置关联藏品");
        }
        BuyFirstCollection oldBuyFirstCollection = buyFirstCollectionService.selectByProductIdAndCollectionId(
                request.getProductId(), request.getCollectionId());
        if (ObjectUtil.isNotNull(oldBuyFirstCollection)) {
            return Result.fail("商品已关联了该藏品不能重复关联");
        }
        if (request.getBuyFirstNum() > digitalCollectionProductInfo.getPriorityPurchaseLimit()) {
            return Result.fail("关联藏品的优先购总次数不能大于商品的优先购次数");
        }
        BuyFirstCollection buyFirstCollection = new BuyFirstCollection();
        buyFirstCollection.setProductId(request.getProductId());
        buyFirstCollection.setCollectionBuyFirstId(request.getCollectionId());
        buyFirstCollection.setBuyFirstNum(request.getBuyFirstNum());
        buyFirstCollection.insert();

        return Result.success();
    }

    @PostMapping(value = "/buyFirstUserList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "优先购商品白名单", notes = "优先购商品白名单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<BuyFirstUserListVO> buyFirstUserList(@Valid @RequestBody BuyFirstUserListRequest request) {
        Page<BuyFirstUserListVO> page = buyFirstUserService.buyFirstUserList(request.getProductId(),
                request.getUserTel(), request.getPageNo(), request.getPageSize());
        return PageResult.success(page);
    }

    @PostMapping(value = "/rushBuyUserList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "优先抢商品白名单", notes = "优先抢商品白名单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<RushBuyUserListVO> rushBuyUserList(@Valid @RequestBody BuyFirstUserListRequest request) {
        Page<RushBuyUserListVO> page = rushBuyUserService.rushBuyUserList(request.getProductId(), request.getUserTel(),
                request.getPageNo(), request.getPageSize());
        return PageResult.success(page);
    }

    @PostMapping(value = "/simpleUserList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "简单用户列表", notes = "简单用户列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<SimpleUserVO> simpleUserList(@Valid @RequestBody SimpleUserRequest request) {
        Page<SimpleUserVO> page = userInfoService.simpleUserList(request.getUserTel(), request.getPageNo(),
                request.getPageSize());
        return PageResult.success(page);
    }

    @PostMapping(value = "/addBuyFirstUser", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "设置优先购商品白名单", notes = "设置优先购商品白名单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result addBuyFirstUser(@Valid @RequestBody AddBuyFirstUserRequest request) {
        DigitalCollectionProductInfo digitalCollectionProductInfo = (DigitalCollectionProductInfo) new DigitalCollectionProductInfo().selectById(
                request.getProductId());
        if (ObjectUtil.isEmpty(digitalCollectionProductInfo)) {
            return Result.fail("未找到商品请稍后重试");
        }
        if (!digitalCollectionProductInfo.getIsPriorityPurchase()) {
            return Result.fail("该商品不是优先购商品不能设置关联藏品");
        }
        BuyFirstUser oldBuyFirstUser = buyFirstUserService.selectByProductIdAndUserId(request.getProductId(),
                request.getUserId());
        if (ObjectUtil.isNotNull(oldBuyFirstUser)) {
            return Result.fail("用户已在商品的白名单中不可重复添加");
        }
        if (request.getBuyFirstNum() > digitalCollectionProductInfo.getPriorityPurchaseLimit()) {
            return Result.fail("白名单优先购次数不能大于商品的优先购次数");
        }
        BuyFirstUser buyFirstUser = new BuyFirstUser();
        buyFirstUser.setProductId(request.getProductId());
        buyFirstUser.setUserBuyFirstId(request.getUserId());
        buyFirstUser.setBuyFirstNum(request.getBuyFirstNum());
        buyFirstUser.insert();
        return Result.success();
    }

    @PostMapping(value = "/addRushBuyUser", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "设置优先抢商品白名单", notes = "设置优先购商品白名单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result addRushBuyUser(@Valid @RequestBody AddBuyFirstUserRequest request) {
        DigitalCollectionProductInfo digitalCollectionProductInfo = (DigitalCollectionProductInfo) new DigitalCollectionProductInfo().selectById(
                request.getProductId());
        if (ObjectUtil.isEmpty(digitalCollectionProductInfo)) {
            return Result.fail("未找到商品请稍后重试");
        }
        if (!digitalCollectionProductInfo.getIsRushBuy()) {
            return Result.fail("该商品不是优先抢商品不能设置关联藏品");
        }
        RushBuyUser oldRushBuyUser = rushBuyUserService.selectByProductIdAndUserId(request.getProductId(),
                request.getUserId());
        if (ObjectUtil.isNotNull(oldRushBuyUser)) {
            return Result.fail("用户已在商品的白名单中不可重复添加");
        }
        if (request.getBuyFirstNum() > digitalCollectionProductInfo.getRushBuyLimit()) {
            return Result.fail("白名单优先抢次数不能大于商品的优先抢次数");
        }
        RushBuyUser rushBuyUser = new RushBuyUser();
        rushBuyUser.setProductId(request.getProductId());
        rushBuyUser.setUserBuyFirstId(request.getUserId());
        rushBuyUser.setBuyFirstNum(request.getBuyFirstNum());
        rushBuyUser.insert();
        return Result.success();
    }

    @PostMapping(value = "/deleteBuyFirstUser", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除优先购商品白名单用户", notes = "删除优先购商品白名单用户", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result deleteBuyFirstUser(@Valid @RequestBody IdRequest request) {
        new BuyFirstUser().deleteById(request.getId());
        return Result.success();
    }

    @PostMapping(value = "/deleteRushBuyUser", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除优先抢商品白名单用户", notes = "删除优先抢商品白名单用户", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result deleteRushBuyUser(@Valid @RequestBody IdRequest request) {
        new RushBuyUser().deleteById(request.getId());
        return Result.success();
    }

    @PostMapping(value = "/addBuyFirstUserExcel", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "批量优先购商品白名单excel", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uploadFile", value = "文件流对象", required = true, dataType = "__File")})
    @ResponseBody
    @SysMemberLogin
    public Result<List<AddBuyFirstUserExcelVO>> addBuyFirstUserExcel(@Valid AddBuyFirstUserExcelRequest request)
            throws GlobalException, IOException {
        List<AddBuyFirstUserExcelVO> list = buyFirstUserService.addBuyFirstUserExcel(request.getProductId(),
                request.getUploadFile());
        return Result.success(list);
    }


    @PostMapping(value = "/batchBuyFirstUser", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "批量添加优先购商品白名单", notes = "批量添加优先购商品白名单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result batchBuyFirstUser(@Valid @RequestBody BatchBuyFirstUserRequest request)
            throws GlobalException, IOException {
        DigitalCollectionProductInfo digitalCollectionProductInfo = (DigitalCollectionProductInfo) new DigitalCollectionProductInfo().selectById(
                request.getProductId());
        if (ObjectUtil.isEmpty(digitalCollectionProductInfo)) {
            return Result.fail("未找到商品请稍后重试");
        }
        if (!digitalCollectionProductInfo.getIsPriorityPurchase()) {
            return Result.fail("该商品不是优先购商品不能设置关联藏品");
        }
        if (request.getBuyFirstNum() > digitalCollectionProductInfo.getPriorityPurchaseLimit()) {
            return Result.fail("白名单优先购次数不能大于商品的优先购次数");
        }
        for (Map map : request.getList()) {
            Long userId = Long.parseLong(map.get("userId").toString());
            BuyFirstUser buyFirstUser = new BuyFirstUser();
            buyFirstUser.setProductId(request.getProductId());
            buyFirstUser.setUserBuyFirstId(userId);
            buyFirstUser.setBuyFirstNum(request.getBuyFirstNum());
            buyFirstUser.insert();
        }
        return Result.success();
    }

    @PostMapping(value = "/batchRushBuyUser", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "批量添加优先抢商品白名单", notes = "批量添加优先抢商品白名单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result batchRushBuyUser(@Valid @RequestBody BatchBuyFirstUserRequest request)
            throws GlobalException, IOException {
        DigitalCollectionProductInfo digitalCollectionProductInfo = (DigitalCollectionProductInfo) new DigitalCollectionProductInfo().selectById(
                request.getProductId());
        if (ObjectUtil.isEmpty(digitalCollectionProductInfo)) {
            return Result.fail("未找到商品请稍后重试");
        }
        if (!digitalCollectionProductInfo.getIsRushBuy()) {
            return Result.fail("该商品不是优先抢商品不能设置关联藏品");
        }
        if (request.getBuyFirstNum() > digitalCollectionProductInfo.getRushBuyLimit()) {
            return Result.fail("优先抢白名单次数不能大于商品的优先抢次数");
        }
        for (Map map : request.getList()) {
            Long userId = Long.parseLong(map.get("userId").toString());
            RushBuyUser rushBuyUser = new RushBuyUser();
            rushBuyUser.setProductId(request.getProductId());
            rushBuyUser.setUserBuyFirstId(userId);
            rushBuyUser.setBuyFirstNum(request.getBuyFirstNum());
            rushBuyUser.insert();
        }
        return Result.success();
    }


    @PostMapping(value = "/rushBuyCollectionsList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "优先抢关联的藏品列表", notes = "优先抢关联的藏品列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<List<RushBuyCollectionsListVO>> rushBuyCollectionsList(@Valid @RequestBody IdRequest request) {
        List<RushBuyCollectionsListVO> list = rushBuyCollectionService.rushBuyCollectionsList(request.getId());
        return Result.success(list);
    }

    @PostMapping(value = "/deleteRushBuyCollections", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除优先抢商品关联的藏品", notes = "删除优先抢商品关联的藏品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result deleteRushBuyCollections(@Valid @RequestBody IdRequest request) {
        new RushBuyCollection().deleteById(request.getId());
        return Result.success();
    }

    @PostMapping(value = "/addRushBuyCollections", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "设置优先抢商品关联的藏品", notes = "设置优先抢商品关联的藏品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result addRushBuyCollections(@Valid @RequestBody AddRushBuyCollectionRequest request) {
        DigitalCollectionProductInfo digitalCollectionProductInfo = (DigitalCollectionProductInfo) new DigitalCollectionProductInfo().selectById(
                request.getProductId());
        if (ObjectUtil.isEmpty(digitalCollectionProductInfo)) {
            return Result.fail("未找到商品请稍后重试");
        }
        if (!digitalCollectionProductInfo.getIsRushBuy()) {
            return Result.fail("该商品不是优先购商品不能设置关联藏品");
        }
        RushBuyCollection oldRushBuyCollection = rushBuyCollectionService.selectByProductIdAndCollectionId(
                request.getProductId(), request.getCollectionId());
        if (ObjectUtil.isNotNull(oldRushBuyCollection)) {
            return Result.fail("商品已关联了该藏品不能重复关联");
        }
        if (request.getBuyFirstNum() > digitalCollectionProductInfo.getRushBuyLimit()) {
            return Result.fail("关联藏品的优先抢次数不能大于商品的优先抢数量");
        }
        RushBuyCollection rushBuyCollection = new RushBuyCollection();
        rushBuyCollection.setProductId(request.getProductId());
        rushBuyCollection.setCollectionBuyFirstId(request.getCollectionId());
        rushBuyCollection.setBuyFirstNum(request.getBuyFirstNum());
        rushBuyCollection.insert();

        return Result.success();
    }

    @PostMapping(value = "/buyCollectionDiscountList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "商品关联的有折扣的藏品列表", notes = "商品关联的有折扣的藏品列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<List<BuyCollectionDiscountListVO>> buyCollectionDiscountList(@Valid @RequestBody IdRequest request) {
        List<BuyCollectionDiscountListVO> list = buyCollectionDiscountService.buyCollectionDiscountList(
                request.getId());
        return Result.success(list);
    }

    @PostMapping(value = "/deleteBuyCollectionDiscount", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除商品关联的有折扣的藏品列表", notes = "删除商品关联的有折扣的藏品列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result deleteBuyCollectionDiscount(@Valid @RequestBody IdRequest request) {
        new BuyCollectionDiscount().deleteById(request.getId());
        return Result.success();
    }

    @PostMapping(value = "/addBuyCollectionDiscount", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "商品绑定藏品设置折扣", notes = "商品绑定藏品设置折扣", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result addBuyCollectionDiscount(@Valid @RequestBody AddBuyCollectionDiscountRequest request) {
        DigitalCollectionProductInfo digitalCollectionProductInfo = (DigitalCollectionProductInfo) new DigitalCollectionProductInfo().selectById(
                request.getProductId());
        if (ObjectUtil.isEmpty(digitalCollectionProductInfo)) {
            return Result.fail("未找到商品请稍后重试");
        }
        BuyCollectionDiscount oldBuyCollectionDiscount = buyCollectionDiscountService.selectByProductIdAndCollectionId(
                request.getProductId(), request.getCollectionId());
        if (ObjectUtil.isNotNull(oldBuyCollectionDiscount)) {
            return Result.fail("商品已关联了该藏品不能重复关联");
        }
        BuyCollectionDiscount buyCollectionDiscount = new BuyCollectionDiscount();
        buyCollectionDiscount.setProductId(request.getProductId());
        buyCollectionDiscount.setCollectionBuyFirstId(request.getCollectionId());
        buyCollectionDiscount.setBuyFirstNum(request.getBuyFirstNum());
        buyCollectionDiscount.insert();

        return Result.success();
    }

    @PostMapping(value = "/getProductDetailAdmin", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查看商品详情", notes = "查看商品详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<GetProductDetailAdminVO> getProductDetailAdmin(@Valid @RequestBody IdRequest request) {
        DigitalCollectionProductInfo digitalCollectionProductInfo = (DigitalCollectionProductInfo) new DigitalCollectionProductInfo().selectById(
                request.getId());
        GetProductDetailAdminVO getProductDetailAdminVO = BeanUtil.toBean(digitalCollectionProductInfo,
                GetProductDetailAdminVO.class);
        ShareProfitPanel panel = shareProfitPanelDao.getById(digitalCollectionProductInfo.getShareProfitPanelId());
        if (ObjectUtil.isNotNull(panel)) {
            getProductDetailAdminVO.setShareProfitPanelId(panel.getId());
            getProductDetailAdminVO.setShareProfitPanelName(panel.getPanelName());
        }
        return Result.success(getProductDetailAdminVO);
    }

    @PostMapping(value = "/getCollectionRule", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查看商品合成规则", notes = "查看藏品合成规则", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<ProductRuleVO> getCollectionRule(@Valid @RequestBody ProductRuleRequest request) {
        Page<ProductRuleVO> page = privilegeRuleService.getProductRule(request.getPageNo(), request.getPageSize(),
                request.getProductId());
        return PageResult.success(page);
    }

    @PostMapping(value = "/getCollectionRuleConsensus", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查看商品合成是否消耗共识度", notes = "查看商品合成是否消耗共识度", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<ProductRuleConsensusVO> getCollectionRuleConsensus(
            @Valid @RequestBody GetPrivilegeRuleConsensusRequest request) {
        return Result.success(privilegeRuleService.getCollectionRuleConsensus(request.getProductId()));
    }

    @PostMapping(value = "/saveCollectionRuleConsensus", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "保存商品合成是否消耗共识度", notes = "保存商品合成是否消耗共识度", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result saveCollectionRuleConsensus(@Valid @RequestBody SaveProductRuleConsensusReq request) {
        request.getEcologyCastGas().checkEcologyCastGas();
        privilegeRuleService.saveCollectionRuleConsensus(request.getProductId(), request.getConsumeConsensus(),request.getEcologyCastGas());
        return Result.success();
    }


    @PostMapping(value = "/addSynthesisProductRule", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "设置商品合成规则", notes = "设置藏品合成规则", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result addSynthesisProductRule(@Valid @RequestBody AddPrivilegeRuleRequest request) {
        DigitalCollectionProductInfo digitalCollectionProductInfo = (DigitalCollectionProductInfo) new DigitalCollectionProductInfo().selectById(
                request.getProductId());
        List<PrivilegeRuleRequest> ruleList = request.getRuleList();
        if (ObjectUtil.isEmpty(digitalCollectionProductInfo)) {
            return Result.fail("未找到该商品请稍后重试");
        }
        if (digitalCollectionProductInfo.getStatus()) {
            return Result.fail("商品上架中不能做设置");
        }
        for (PrivilegeRuleRequest privilegeRuleRequest : ruleList) {
            DigitalCollection digitalCollection = (DigitalCollection) new DigitalCollection().selectById(
                    privilegeRuleRequest.getConsumeCollectionId());
            List<PrivilegeRule> list1 = privilegeRuleService.getByProductIdAndConsumeId(request.getProductId(),
                    privilegeRuleRequest.getConsumeCollectionId());
            if (list1.size() > 0) {
                return Result.fail("消耗的藏品已存在请勿重复添加");
            }
            PrivilegeRule privilegeRule = new PrivilegeRule();
            privilegeRule.setProductId(request.getProductId());
            privilegeRule.setCollectionId(digitalCollectionProductInfo.getRelationId());
            privilegeRule.setConsumeCollectionId(digitalCollection.getId());
            privilegeRule.setConsumeCollectionName(digitalCollection.getFirstTitle());
            privilegeRule.setConsumeCollectionLevel(digitalCollection.getLevel().getCode());
            privilegeRule.setConsumeCollectionNum(privilegeRuleRequest.getConsumeCollectionNum());
            privilegeRule.setIsDestroy(privilegeRuleRequest.getIsDestroy());
            privilegeRule.insert();
        }
        return Result.success();
    }

    @PostMapping(value = "/updateProductRule", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改商品合成规则", notes = "修改合成规则", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result updateProductRule(@Valid @RequestBody UpdateRuleRequest request) {
        DigitalCollectionProductInfo digitalCollectionProductInfo = (DigitalCollectionProductInfo) new DigitalCollectionProductInfo().selectById(
                request.getProductId());
        if (digitalCollectionProductInfo.getStatus()) {
            return Result.fail("商品上架中不能做修改");
        }
        PrivilegeRule privilegeRule = new PrivilegeRule();
        privilegeRule.setId(request.getId());
        privilegeRule.setConsumeCollectionNum(request.getNumber());
        privilegeRule.setIsDestroy(request.getIsDestroy());
        privilegeRule.updateById();
        return Result.success();
    }

    @PostMapping(value = "/deleteProductRule", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除商品合成规则", notes = "删除商品合成规则", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result deleteProductRule(@Valid @RequestBody DeleteProductRuleRequest request) {
        DigitalCollectionProductInfo digitalCollectionProductInfo = (DigitalCollectionProductInfo) new DigitalCollectionProductInfo().selectById(
                request.getProductId());
        if (digitalCollectionProductInfo.getStatus()) {
            return Result.fail("商品上架中不能删除");
        }
        new PrivilegeRule().deleteById(request.getId());
        return Result.success();
    }


    @PostMapping(value = "/getPushList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取推送列表", notes = "获取推送列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<ArrayList<PushDetailsVO>> getPushList(@Valid @RequestBody IdRequest request) {
        return Result.success(pushDetailsService.getPushList(request.getId()));
    }

    @PostMapping(value = "/pushProduct", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "推送商品", notes = "推送商品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result pushProduct(@Valid @RequestBody PushProductRequest request) {
        pushDetailsService.pushProduct(request.getId(), request.getList());
        return Result.success();
    }


    @PostMapping(value = "/getAssociateProductListByCommodityType", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取关联的NFT商品(按类型区分)(上架中)", notes = "获取关联的NFT商品(按类型区分)(上架中)", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<AssociateProductListByCommodityTypeVO> getAssociateProductListByCommodityType(
            @Valid @RequestBody AssociateProductListByCommodityTypeRequest request) {
        return PageResult.success(
                digitalCollectionProductInfoService.getAssociateProductListByCommodityType(request.getCommodityType(),
                        request.getPageNo(), request.getPageSize(), request.getFirstTitle(),
                        request.getTypeClassify()));
    }

    @PostMapping(value = "/getAllAssociateProductListByCommodityType", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取关联的NFT商品(按类型区分)(所有)", notes = "获取关联的NFT商品(按类型区分)(所有)", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<AssociateProductListByCommodityTypeVO> getAllAssociateProductListByCommodityType(
            @Valid @RequestBody AssociateProductListByCommodityTypeRequest request) {
        return PageResult.success(digitalCollectionProductInfoService.getAllAssociateProductListByCommodityType(
                request.getCommodityType(), request.getPageNo(), request.getPageSize(), request.getFirstTitle(),
                request.getTypeClassify()));
    }

    @PostMapping(value = "/getAssociateChipProductList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取关联的NFT商品芯片类型", notes = "获取关联的NFT商品芯片类型", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<AssociateProductListByCommodityTypeVO> getAssociateProductListByCommodityType(
            @Valid @RequestBody AssociateChipProductRequest request) {
        return PageResult.success(digitalCollectionProductInfoService.getAssociateChipProductList(request.getPageNo(),
                request.getPageSize(), request.getFirstTitle()));
    }


    @PostMapping(value = "/addSubscribeProduct", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "创建申购商品", notes = "创建申购商品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result addSubscribeProduct(@Valid @RequestBody AddSubscribeProductDTO request) {
        digitalCollectionProductInfoService.addSubscribeProduct(request);
        return Result.success();
    }


    @PostMapping(value = "/modificationSubscribeProduct", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改申购商品", notes = "修改申购商品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result modificationSubscribeProduct(@Valid @RequestBody ModificationSubscribeProductDTO request) {
        digitalCollectionProductInfoService.modificationSubscribeProduct(request);
        return Result.success();
    }

    @PostMapping(value = "/getSubscribeProductById", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "根据id获取申购商品", notes = "根据id获取申购商品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<SubscribeProductAdminVO> getSubscribeProductById(@Valid @RequestBody IdRequest request) {
        return Result.success(digitalCollectionProductInfoService.getSubscribeProductById(request.getId()));
    }

    @PostMapping(value = "/addAuctionProduct", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "创建竞拍商品", notes = "创建竞拍商品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result addAuctionProduct(@Valid @RequestBody AddAuctionProductDTO request) {
        digitalCollectionProductInfoService.addAuctionProduct(request);
        return Result.success();
    }

    @PostMapping(value = "/modifyAuctionProduct", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改竞拍商品", notes = "修改竞拍商品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result modifyAuctionProduct(@Valid @RequestBody ModifyAuctionProductDTO request) {
        digitalCollectionProductInfoService.modifyAuctionProduct(request);
        return Result.success();
    }

    @PostMapping(value = "/getAuctionProductById", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "根据id获取竞拍商品", notes = "根据id获取竞拍商品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<AuctionProductAdminVO> getAuctionProductById(@Valid @RequestBody IdRequest request) {
        return Result.success(digitalCollectionProductInfoService.getAuctionProductById(request.getId()));
    }


}
